﻿using MvcApplication2.Bussiness;
using MvcApplication2.Filters;
using MvcApplication2.Models;
using MvcApplication2.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication2.Controllers
{
    public class EmployeeController : Controller
    {
        //
        // GET: /Test/
        [Authorize]
        [HeaderFooterFilter]
        public ActionResult Index()
        {
            return GetView();
        }

        public ActionResult GetView()
        {
            EmployeeBusinessLayer embuss=new EmployeeBusinessLayer();
            EmployeeListViewModel emlist = new EmployeeListViewModel();
           // emlist.UserName =User.Identity.Name;
            List<Employee> emli= embuss.GetEmployee();
            List<EmployeeViewModel> list = new List<EmployeeViewModel>();
            foreach (Employee em in emli)
            {
                EmployeeViewModel emvm = new EmployeeViewModel();
                emvm.EmployeeName = em.FirstName + em.LastName;
                emvm.EmpID = em.EmployeeID.ToString();
                emvm.Salary = em.Salary;
                if (emvm.Salary > 100)
                {
                    emvm.SalaryColor = "green";
                }
                else
                {
                    emvm.SalaryColor = "yellow";
                }
                list.Add(emvm);
            }
            emlist.list = list;
         
            return View("Index", emlist);
        }
        public string empID = "";
            [AdminFilter]
        [HeaderFooterFilter]
        public ActionResult DeleteEmployeess()
        {
             empID = Request["data"].ToString();
             ViewData["empid"] = empID;
            return View("DeleteEm");
        }
      
        [AdminFilter]
        [HeaderFooterFilter]
        public ActionResult AddNew()
        {
            return View("CreateEmployee",new CreateEmployeeViewModel());
        }
        [AdminFilter]
        [HeaderFooterFilter]
        public ActionResult DeleteEmployee(string emp)
        {
            string empID = Request["data"].ToString();
            //string empID = emp;
            EmployeeBusinessLayer eb = new EmployeeBusinessLayer();
            eb.DeleteEmployee(empID);
            return RedirectToAction("Index");
        }
           [AdminFilter]
           [HeaderFooterFilter]
        public ActionResult SaveEmployee(Employee e,string BtnSubmit)
        {
            switch (BtnSubmit)
            {
                case "Save":
                    if (ModelState.IsValid)
                    {
                        EmployeeBusinessLayer eb = new EmployeeBusinessLayer();
                        eb.SaveEmployee(e);
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        CreateEmployeeViewModel cv = new CreateEmployeeViewModel();
                        cv.FirstName = e.FirstName;
                        cv.LastName = e.LastName;
                        cv.Salart = e.Salary;
                        return View("CreateEmployee",cv);
                    }
                   
                case "取消":
                    return RedirectToAction("Index");
                  
            }
            return new EmptyResult();
        }
        public ActionResult GetAddNewLink()
        {
            if (Convert.ToBoolean(Session["IsAdmin"]))
            {
                return PartialView("AddNewLink");
            }
            else
            {
                return new EmptyResult();
            }
        }
    }
}
